package com.example.myapp61.Mfragment.Home;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapp61.R;
import com.example.myapp61.Util.ToasUtil;

import java.util.ArrayList;

import static android.content.ContentValues.TAG;

/**
 * RecyclerView适配器
 */
public class SmartAdapter extends RecyclerView.Adapter<SmartAdapter.Myhodel> {
    private Context mcontext;
    public ArrayList mDatas;
    OnItemClick onItemClick;
    private ArrayList<XList> mDatas2;
    public ArrayList<XList> getDatas2() {
        return mDatas2;
    }

    public SmartAdapter(Context mcontext) {
        this.mcontext = mcontext;
    }

    //    public SmartAdapter(Context context, ArrayList par){
//        this.mcontext = context;
//        this.mDatas = par;
//    }
    public SmartAdapter(Context context, ArrayList<XList> params){
        this.mcontext = context;
        this.mDatas2 = params;
    }

    /**
     * 单数据增加
     * @param position
     */
//    public void add(int position){
//        mDatas2.add(position,);
//        notifyItemInserted(position);
//    }

    /**
     * 多数据增加----用于刷新
     * @param arrayList
     */
    public void addall(ArrayList arrayList){
        if (arrayList!=null){
            int num = mDatas2.size();
            mDatas2.clear();
            notifyItemRangeRemoved(0,num);
            mDatas2.addAll(arrayList);
            notifyItemRangeInserted(0,arrayList.size());
        }
    }

    public void saveall(ArrayList<XList> arrayList){
        mDatas2.addAll(arrayList);
        notifyItemRangeInserted(mDatas2.size(),arrayList.size());
    }
    /**
     * 删除数据
     * @param position
     */
    public void delete(int position){
        mDatas2.remove(position);
        notifyItemRemoved(position);
    }
    public void ref(ArrayList arr){
        mDatas.clear();
        mDatas.addAll(arr);
        notifyItemRangeInserted(mDatas2.size(),arr.size());
    }

    /**
     * 点击事件
     */
    public interface OnItemClick{
        void click(View view, int position);
        void longClick(View view, int position);
    }

    /**
     * 设置事件
     * @param onItemClick
     */
    public void setOnItemClick(OnItemClick onItemClick){
        this.onItemClick = onItemClick;
    }

    @Override
    public int getItemViewType(int position) {
        return mDatas2.get(position).getLabel();
    }

    @NonNull
    @Override
    public Myhodel onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view1 = LayoutInflater.from(mcontext).inflate(R.layout.adapter_item, parent,false);
        View view2 = LayoutInflater.from(mcontext).inflate(R.layout.adapter_item2, parent,false);
        if (viewType==1){
            return new Myhodel(view1);
        }else{
            return new Myhodel(view2);
        }
    }



    @Override
    public void onBindViewHolder(final Myhodel holder, final int position) {
        holder.textView.setText(mDatas2.get(position).getMessages());
        //Log.d(TAG, "onBindViewHolder: "+mDatas2.get(position).getImages().toString());
//        if (mDatas2.get(position).getImages().isEmpty()){
//            Log.d("测试", "onBindViewHolder: "+mDatas2.get(position).getImages().toString());
//        }
        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClick.click(holder.itemView,holder.getLayoutPosition());
            }
        });
        holder.textView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                onItemClick.longClick(holder.itemView,holder.getLayoutPosition());
                return false;
            }
        });
    }
    @Override
    public int getItemCount() {
        return mDatas2.size();
    }

    class Myhodel extends RecyclerView.ViewHolder{
        private TextView textView;
        public Myhodel(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.adapter_item);
        }
    }
}
